$(function () {
    $("#emp_datagrid").datagrid({
        toolbar: '#emp_toolbar',
        url: '/report/list.do',
        fit: true,
        fitColumns: true,
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '报案人姓名', field: 'name',align:'center',width:100},
            {title: '性别', field: 'gender' ,align:'center',width:100,formatter: function (value, row, index) {
                    if(value == 0){
                        return "男";
                    }else{
                        return "女";
                    }}},
            {title: '电话', field: 'tel' ,align:'center',width:100},
            {title: '事故日期', field: 'happendate' ,align:'center',width:100},
            {title: '事故地点', field: 'place' ,align:'center',width:100},
            {title: '保单编号', field: 'getguard' ,align:'center',width:100},
            {title: '车牌号', field: 'carnumber' ,align:'center',width:100},
            {
                title: '录入人', field: 'employee' ,align:'center',width:100, formatter: function (value, row, index) {
                    return value ? value.username : "";
                }
            },
            {
                title: '当前状态', field: 'state' ,align:'center',width:100, formatter: function (value, row, index) {
                    if(value == 0){
                        return "<font color='#8b0000'>保单不存在</font>";
                    } else if(value == 1){
                        return "<font color='#8b0000'>保单已失效</font>";
                    } else if(value == 2) {
                        return "<font color='red'>待受理</font>";
                    } else {
                        return "<font color='#6495ed'>已受理</font>";
                    }
                }
            },
        ]],

    })

    $("#emp_dialog").dialog({
        width: 350,
        height: 380,
        buttons: '#emp_buttons',
        closed: true,
        onClose: function () {
            $("#emp_form").form("clear")
        }
    })
    $("#report_dialog").dialog({
        width: 350,
        height: 380,
        closed: true,
        buttons:'#ok'
    })


    var methodObj = {
        //添加按钮
        add: function () {
            $("#emp_dialog").dialog("open");
            $("#tr_password").show();
            $("#emp_dialog").dialog('setTitle', '案件新增');
        },
        //编辑按钮
        edit: function () {
            var data = $("#emp_datagrid").datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            $("#tr_password").hide();

            $("#emp_form").form('load', data);

            $("#emp_dialog").dialog("open");
            $("#emp_dialog").dialog('setTitle', '案件编辑');
        },
        //受理按钮
        accept: function () {
            var data = $("#emp_datagrid").datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            else if(data.state==0||data.state==1){
                $.messager.alert('温馨提示', "不存在该保单");
                return;
            }
            else if(data.state==3){
                $.messager.alert('温馨提示', "请选择未受理案件");
                return;
            } else {
                //$("#emp_form").form('load', data);
                if(data.report){
                    data["report.id"]=data.report.id;
                    data["report.username"]=data.report.username;
                }
                $.messager.confirm("温馨提示","确定要受理该案件吗?",function (d) {
                    $.post("/accdentdetail/insertCase.do",{carnumber:data.carnumber, tel:data.tel,
                        getguard:data.getguard, place:data.place, name:data.name},function (r) {
                        if (r.success) {
                            $.get("/report/changeState.do",{id:data.id});
                            $.messager.alert("温馨提示", "操作成立,已受理", "info", function () {
                                $("#emp_datagrid").datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                        $.get("/report/changeState.do",{id:data.id},function () {

                        })
                        $("#emp_datagrid").datagrid("reload");
                        return;
                    })
                })

            }

        },

        //刷新按钮
        reload: function () {
            $("#emp_datagrid").datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            $("#emp_dialog").dialog("close");
        },
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");

            $("#emp_datagrid").datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate
            });
        },
        //添加保存按钮
        save: function (val) {
            var url = $(val).data("url");
            var from = $(val).data("from");
            $("#emp_form").form('submit', {
                url: '/report/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            $("#emp_datagrid").datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                },

            })
        },

    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd](this);
    })

})

